package edu.luc.cs.backend;
import java.io.IOException;
import java.util.*;

public class UniversitySystem {
	private ArrayList<Student> students;
	private ArrayList<Employee> staff;
	private ArrayList<CourseSection> courseCatalog;
	private String currentUser;
	
	public UniversitySystem() {
		students = new ArrayList<Student>();
		staff = new ArrayList<Employee>();
		courseCatalog = new ArrayList<CourseSection>();
		
		//test data//
		System.out.println("Adding dummy data...");
		staff.add(new Employee("Doe", "John", 1976, 3, 28, "male"));
		staff.add(new Employee("Doe", "Jane", 1986, 3, 28, "female"));
		students.add(new Student("Queue", "Susie", 1965, 6, 15, "female"));
		students.add(new Student("Bun", "Christopher", 1995, 6, 15, "male"));
		courseCatalog.add(new CourseSection("COMP488", "Intermediate Object-Oriented Programming",
				"Computer software development or programming, using the object-oriented paradigm", "Spr2009"));
		courseCatalog.add(new CourseSection("COMP451", "Distributed Systems", "Resource sharing and openness", "Spr2009"));
		assignProfessorToCourse("E0", "C0");
		
	}
	
	public ArrayList<Student> getStudents() {
		return students;
	}
	public void setStudents(ArrayList<Student> students) {
		this.students = students;
	}
	public void addStudent(Student s) {
		students.add(s);
	}
	public void addStudent(String familyName, String givenName, int yyyy, int mm,
			int dd, String sex) {
		addStudent(new Student(familyName, givenName, yyyy, mm, dd, sex));
	}
	public void removeStudent(Student s) {
		students.remove(students.indexOf(s));
		System.out.println("Student removed");
	}

	public void removeStudent(String uvid) {
		try {
			students.remove(getStudent(uvid));
			System.out.println("Student removed");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Student getStudent(String uvid) throws IOException {
		Student temp = null;
		for (int i = 0; i < students.size(); i++) {
			if (students.get(i).getUvid().equals(uvid)) {
				temp = students.get(i);
			}
			else throw new IOException();
		}
		return temp;
	}
	
	public void addEmployee(Employee e) {
		staff.add(e);
		System.out.println("Emp added");
	}
	
	public void addEmployee(String familyName, String givenName, int yyyy, int mm,
			int dd, String sex){
		addEmployee(new Employee(familyName, givenName, yyyy, mm, dd, sex));
	}
	
	public void removeEmployee(Employee e) {
		staff.remove(staff.indexOf(e));
		System.out.println("Emp removed");
	}
	
	public void removeEmployee(String staffId){
		try {
			staff.remove(getEmployee(staffId));
			System.out.println("Emp removed");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Employee getEmployee(String staffId) throws IOException {
		Employee temp = null;
		for (int i = 0; i < staff.size(); i++) {
			
			if (staff.get(i).getStaffId().equals(staffId)) {
				System.out.println("Getting employee " + staff.get(i).getStaffId());
				temp = staff.get(i);
			}
		}
		if (temp == null) throw new IOException();
		return temp;
	}
	
	public ArrayList<Employee> getStaff() {
		return staff;
	}
	
	public void addCourseSection(CourseSection cs) {
		courseCatalog.add(cs);
	}
	
	public void removeCourseSection(CourseSection cs) {
		courseCatalog.remove(courseCatalog.indexOf(cs));
	}
	
	public void removeCourseSection(String csid) {
		try {
			removeCourseSection(getCourseSection(csid));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public CourseSection getCourseSection (String csid) throws IOException {
		CourseSection temp = null;
		for (int i = 0; i < courseCatalog.size(); i++) {
			if (courseCatalog.get(i).getCsid().equals(csid)) {
				temp = courseCatalog.get(i);
			}
		}
		if (temp == null) throw new IOException();
		return temp;
	}
	
	public ArrayList<CourseSection> getCourseCatalog() {
		return courseCatalog;
	}

	public void setCurrentUser(String currentUser) {
		this.currentUser = currentUser;
	}

	public String getCurrentUser() {
		return currentUser;
	}
	
	public void assignProfessorToCourse(String staffId, String courseId) {
		try {
			getEmployee(staffId).addCourse(courseId);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			getCourseSection(courseId).setProfessor(getEmployee(staffId));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
